package com.itcast.controller;

import com.itcast.pojo.service.Category;
import com.itcast.service.SubCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SubCategoryController {

    @Autowired
    private SubCategoryService subCategoryService;

    /**
     * 根据pid查询子分类
     */
    @GetMapping("/addSubCategory")
    public ResponseEntity<List<Category>> findSubCategory(@RequestParam("pid") Integer pid) {
        List<Category> categoryList = subCategoryService.findSubCategory(pid);
        return ResponseEntity.ok(categoryList);
    }

    /**
     * 条件查询
     */
    @GetMapping("/subCategory")
    public ResponseEntity<List<Category>> findSubCategory(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam (value = "pid") Integer pid) {
        List<Category> subCategory = subCategoryService.findSubCategoryByCodeOrName(code, name,pid);
        return ResponseEntity.ok(subCategory);
    }


    /**
     * 添加
     */
    @PostMapping("/addSubCategory")
    public ResponseEntity<Void> addSubCategory(@RequestBody Category category){
        subCategoryService.addSubCategory(category);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteCategoryById")
    public ResponseEntity<Void> deleteCategory(@RequestParam ("id") Integer id){
        subCategoryService.deleteSubCategory(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 修改子分类
     */
    @PostMapping("/updateSubCategory")
    public ResponseEntity<Void> updateSubCategory(@RequestBody Category category){
        subCategoryService.updateSubCategory(category);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
